package com.juyu.miliao.api.util;

import java.time.*;

/**
 * 个人资料的显示时间（“刚刚，几分钟前，几小时前，几天前”）
 * 的方法
 * @Auther: 卜加超
 * @Date: 2018/12/16 13:51
 * @Description:
 */
public class ShowTimeUtil {
    public static String getShowTime(LocalDateTime visitorTime){
        //获取当前时间戳
        Long currentTime =System.currentTimeMillis();
        //获取访问时间的时间戳
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = visitorTime.atZone(zone).toInstant();
        long visitorTimes = instant.toEpochMilli();
         //得到时间差
        long differenceTime = Math.abs(currentTime - visitorTimes);
        String showTime = "";
        if(differenceTime<60*1000){
            showTime = "刚刚";
        }else if(differenceTime>60*1000 && differenceTime<60*60*1000){
            showTime = (int)Math.floor(differenceTime/60/1000)+"分钟前";
        }else if(differenceTime>60*60*1000 && differenceTime<60*60*24*1000){
            showTime =(int) Math.floor(differenceTime/60/60/1000)+"小时前";
        }else if(differenceTime>60*60*24*1000 && differenceTime<60*60*24*3*1000){
            showTime = (int)Math.floor(differenceTime/60/60/24/1000)+"天前";
        }else if(differenceTime>= 60*60*24*3*1000){
            showTime = "3天前";
        }
        return showTime;
    }
}
